﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HF.WorkFlow.Engine;
using HF.WorkFlow.Template;
using HF.WorkFlow.Const;
using HF.Framework.SqlDataContract;
using HF.Framework.DataClientDBAgent;

public partial class BasePages_Tooltip : System.Web.UI.Page
{
    string worktaskInsId = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        InitData();
    }
    private void InitData()
    {
        if (Request["WorkTaskInstanceId"] != null)
        {
            string title = "";
            string TaskToWhoMsg = "";
            string ResultMsg = "";
            string OperatorInstanceId = "";
            worktaskInsId = Request["WorkTaskInstanceId"].ToString();
            TaskToWhoMsg = GetTaskToWhoMsg(worktaskInsId);
            //if (string.IsNullOrEmpty(TaskToWhoMsg))
            //{
            //    DataTable dt = WorkTaskInstance.GetInstance().GetWorkTaskInstanceView(worktaskInsId);
            //    if (dt.Rows.Count > 0)
            //    {
            //        OperatorInstanceId = dt.Rows[0]["OperatorInstanceId"].ToString();
            //    }
            //    if(!string.IsNullOrEmpty(OperatorInstanceId))
            //    {
            //        WorkFlowRuntime wf = new WorkFlowRuntime();
            //        string UserId = User.Identity.Name.ToString();
            //        if (UserId != "admin")
            //        {
            //            wf.TaskAssign(UserId, "admin", OperatorInstanceId);
            //            InitData();
            //        }
            //    }
            //}

            ResultMsg = WorkTaskInstance.GetResultMsg(worktaskInsId);

            title = "操作结果:"+ResultMsg;
            if (TaskToWhoMsg.Length <= 0)
            {
                TaskToWhoMsg = "未提交至任何人,请检查流程模板和组织机构配置是否正确!";
                if (ResultMsg == WorkFlowConst.WorkflowOverMsg)//流程结束
                {
                    TaskToWhoMsg = "流程结束!";
                }
                if (ResultMsg == WorkFlowConst.TaskBackMsg)
                {
                    TaskToWhoMsg = WorkFlowConst.TaskBackMsg;
                }
            }
           
            TaskToWhoMsg = "成功提交至:" + TaskToWhoMsg+"。你已完成该任务处理,可以关闭该窗口。";

            System.Text.StringBuilder builder = new System.Text.StringBuilder();
            builder.Append("<script>\r\n");
            builder.Append("setTimeout('Finish();',1000);\r\n");
            builder.Append("function Finish(){\r\n");
            builder.Append("document.all('Image2').src='../images/home.gif';\r\n");
            builder.Append("document.all('" + this.lbTitle.ClientID + "').innerText='" + title + "';\r\n");
            builder.Append("document.all('" + this.lbDescription.ClientID + "').innerText='" + TaskToWhoMsg + "';\r\n");
            //builder.Append("alert('ok');\r\n");
            builder.Append("clearTimeout();\r\n");
            builder.Append("}\r\n");
            builder.Append("</script>\r\n");
            this.Page.RegisterClientScriptBlock("test", builder.ToString());
        }
    }
    
    protected void Button1_Click1(object sender, EventArgs e)
    {
        string title = "ok";
        string description = "yes";
        System.Text.StringBuilder builder = new System.Text.StringBuilder();
        builder.Append("<script>\r\n");
        builder.Append("setTimeout('Finish();',1000);\r\n");
        builder.Append("function Finish(){\r\n");
        builder.Append("document.all('Image2').src='../images/home.gif';\r\n");
        builder.Append("document.all('" + this.lbTitle.ClientID + "').innerText='" + title + "';\r\n");
        builder.Append("document.all('" + this.lbDescription.ClientID + "').innerText='" + description + "';\r\n");
       // builder.Append("alert('ok');\r\n");
        builder.Append("clearTimeout();\r\n");
        builder.Append("}\r\n");
        builder.Append("</script>\r\n");
        this.Page.RegisterClientScriptBlock("test", builder.ToString());
    }

    public static string GetTaskToWhoMsg(string workTaskInstanceId)
    {
        try
        {
            DataTable dt = null;
            int inum = 0;
            string result = ""; int taskType; string operContentText = "";
            for (int i = 0; inum <= 0; i++)
            {

                string sql = "select * from HF_WorkTaskInstanceView  where PreviousTaskInstanceId= @worktaskInsId " +
                              " and (operstatus='0' or operstatus is null) order by opertype";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = sql;
                sqlItem.AppendParameter("@worktaskInsId", workTaskInstanceId);
                ClientDBAgent agent = new ClientDBAgent();
                dt = agent.ExecuteDataTable(sqlItem);
                if (dt.Rows.Count > 0)
                {
                    inum = 1;
                }
                if (dt.Rows.Count == 0)
                {
                    string sqlto = "select WorkTaskInstanceId from HF_WorkTaskInstanceView where PreviousTaskInstanceId='"+workTaskInstanceId+"'";
                    sqlItem.CommandText = sqlto;
                    string worktaskinsid = agent.ExecuteScalar(sqlItem);
                    if (worktaskinsid != "")
                    {
                        workTaskInstanceId = worktaskinsid;
                    }
                    else
                    {
                        inum = 1;
                    }
                }
            }
            if (dt != null & dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    taskType = Convert.ToInt16(dt.Rows[i]["tasktypeid"].ToString());
                    switch (taskType)
                    {
                        case WorkFlowConst.TaskType_SubWorkFlow:
                            operContentText = "进入子流程" + ",";
                            break;
                        case WorkFlowConst.TaskType_End:
                            operContentText = "流程正常结束" + ",";
                            break;
                        case WorkFlowConst.TaskType_Judge:
                            operContentText = "控制节点" + ",";
                            break;
                        default:
                            operContentText = dt.Rows[i]["operContentText"].ToString() + ",";
                            break;
                    }

                    result = result + operContentText;
                }
                result = result.TrimEnd(',');
                //result = result + dt.Rows[dt.Rows.Count - 1]["operContentText"].ToString();
            }
            return result;

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    
}
